package game.module.qzmj.processor;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.common.PlayingRoleMsgProcessor;
import game.entity.PlayingRole;
import game.module.club.logic.ClubRoomManager;
import game.module.hall.logic.CheatManager;
import game.module.qzmj.bean.QzmjGoldCfg;
import game.module.qzmj.bean.QzmjRoom;
import game.module.qzmj.bean.QzmjRoomCfg;
import game.module.qzmj.dao.QzmjGoldTemplateCache;
import game.module.qzmj.dao.QzmjRoomTemplateCache;
import game.module.qzmj.logic.QzmjDataManager;
import game.module.qzmj.logic.QzmjLogic;
import game.util.CommUtil;
import lion.common.MsgCodeAnn;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lua.LuaMessageLogin.S2ErrorCode;
import lua.LuaMessageQzmj.C2SQzmjCreateRoom;
import lua.LuaMessageQzmj.S2CQzmjCreateRoom;

@MsgCodeAnn(msgcode = C2SQzmjCreateRoom.id, accessLimit = 500)
public class QzmjCreateRoomProcessor extends PlayingRoleMsgProcessor {

	private static Logger logger = LoggerFactory.getLogger(QzmjCreateRoomProcessor.class);

	@Override
	public void process(PlayingRole hero, RequestMessageRaw request) throws Exception {
		// TODO Auto-generated method stub
		C2SQzmjCreateRoom reqMsg = C2SQzmjCreateRoom.parse(request);
		int round_index = reqMsg.round_index;
		byte sanjindao = reqMsg.sanjindao;
		byte eat_card = reqMsg.eat_card;
		byte hua_card = reqMsg.hua_card;
		byte prevent_cheat = reqMsg.prevent_cheat;
		byte gold_coin = reqMsg.gold_coin;
		int gold_rate = reqMsg.gold_rate;
		byte cost_type = reqMsg.cost_type;
		byte youjin = reqMsg.youjin;
		int playerId = hero.getId();
		int gold_coin_rate = 0;

		logger.info("qzmj create room,sanjindao={},eat_card={},hua_card={},prevent_cheat={},gold_coin={},gold_rate={}", sanjindao, eat_card, hua_card, prevent_cheat, gold_coin,
				gold_rate);

		if (!QzmjRoomTemplateCache.getInstance().containsConfigIndex(round_index)) {
			S2ErrorCode respMsg = new S2ErrorCode(101);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}

		boolean isInRoom = QzmjDataManager.getInstance().isPlayerInRoom(playerId);
		if (isInRoom) {
			S2ErrorCode respMsg = new S2ErrorCode(102);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		QzmjRoomCfg mjRoomCfg = QzmjRoomTemplateCache.getInstance().getTemplate(round_index);

		int costFangka = mjRoomCfg.getCost_fangka();
		if (hero.getPlayerBean().getFang_ka() < costFangka) {
			S2ErrorCode respMsg = new S2ErrorCode(103);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		if (gold_coin - 1 >= 0) {
			QzmjGoldCfg coinCfg = QzmjGoldTemplateCache.getInstance().getTemplate(gold_coin - 1);
			gold_coin_rate = coinCfg.getGold_coin();
		}
		if (gold_rate != 0) {
			gold_coin_rate = gold_rate;
		}
		QzmjRoom mjroom = QzmjLogic.getInstance().createMjRoom(playerId, mjRoomCfg.getRound(), sanjindao, eat_card, hua_card, prevent_cheat, gold_coin_rate, cost_type, youjin);
		mjroom.setFee((short) costFangka);
		CheatManager.getInstance().openCheatComm(mjroom.getPrevent_cheat(), mjroom.getIps(), hero);
        Map<Integer,String> ipmaps = new HashMap<Integer,String>();
        ipmaps.put(hero.getId(), CommUtil.getIpAddr(hero.getGamePlayer().getAddress()));
        mjroom.setIpmaps(ipmaps);
		ClubRoomManager.getInstance().openClubRoom(hero, mjroom);
		S2CQzmjCreateRoom respMsg = new S2CQzmjCreateRoom();
		respMsg.room_id = mjroom.getUuid();
		respMsg.round_count = mjroom.getRoundCount();
		respMsg.sanjindao = mjroom.getSanjindao();
		respMsg.eat_card = mjroom.getEat_card();
		respMsg.hua_card = mjroom.getHua_card();
		respMsg.prevent_cheat = mjroom.getPrevent_cheat();
		respMsg.gold_rate = mjroom.getGold_coin();
		respMsg.cost_type = mjroom.getCost_type();
		respMsg.youjin = mjroom.getYoujin();
		respMsg.ip = CommUtil.getIpAddr(hero.getGamePlayer().getAddress());
		hero.writeAndFlush(respMsg.build(hero.alloc()));
		logger.info("qzmj create room,sanjindao={},eat_card={},hua_card={},prevent_cheat={},gold_num={}", respMsg.sanjindao, respMsg.eat_card, respMsg.hua_card,
				respMsg.prevent_cheat, respMsg.gold_rate);
	}

	@Override
	public void processProto(PlayingRole playingRole, RequestProtoMessage request) throws Exception {

	}

	@Override
	public void processWebsocket(PlayingRole playingRole, MyRequestMessage request) throws Exception {

	}

	public String getIpAddr(String ipAddrRaw) {
		String retStr = ipAddrRaw;
		int fenIndex = ipAddrRaw.indexOf(":");
		if (fenIndex >= 0) {
			retStr = retStr.substring(0, fenIndex);
		}
		if (retStr.startsWith("/")) {
			retStr = retStr.substring(1);
		}
		return retStr;
	}

	public static void main(String[] args) {
		String ip = "/192.168.0.221：53078";
		String newip = ip.substring(1, 14);
		logger.info("newip=" + newip);
	}

}
